package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.UserMapper;
import com.example.demo.entity.User;
import com.example.demo.dao.UserMapper;
import com.example.demo.exception.ServiceException;
import com.example.demo.service.UserService;
import com.example.demo.util.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wms
 * @since 2022-07-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Override
    public IPage pageC(IPage<User> page) {
        return userMapper.pageC(page);
    }

    @Override
    public IPage pageCC(IPage<User> page, Wrapper wrapper) {
        return userMapper.pageCC(page,wrapper);
    }

    @Override
    public User login(User user) {
        User one = getUserInfo(user);
        if (one != null) {
            BeanUtil.copyProperties(one, user, true);
            // 设置token
            String token = TokenUtils.genToken(one.getId().toString(), one.getPassword());
            user.setToken(token);
        }
        return user;
    }

    @Override
    public User register(User user) {
        return null;
    }

    private User getUserInfo(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("no", user.getNo());
        queryWrapper.eq("password", user.getPassword());
        User one;
            one = getOne(queryWrapper); // 从数据库查询用户信息
        return one;
    }
}
